package org.micode.common.config;

import org.micode.common.codec.SnowflakeCodec;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;

@Configuration
public class SnowflakeConfigurer {

    private static final String WORKER_ID_KEY = "snowflake.workerId";

    private static final String DATA_CENTER_ID_KEY = "snowflake.datacenterId";

    private static final String OFFSET_KEY = "snowflake.offset";

    @DependsOn("springContextHolder")
    @Bean(name = "snowflakeCodec")
    public SnowflakeCodec getSnowflakeCodec() {
        long workerId = ApplicationConfig.readLong(WORKER_ID_KEY, SnowflakeCodec.DEFAULT_WORKER_ID);
        long dataCenterId = ApplicationConfig.readLong(DATA_CENTER_ID_KEY, SnowflakeCodec.DEFAULT_DATA_CENTER_ID);
        Long offset = ApplicationConfig.readLong(OFFSET_KEY, SnowflakeCodec.DEFAULT_BEGIN_OFFSET);
        return new SnowflakeCodec(workerId, dataCenterId, offset);
    }

}
